package com.open.capacity.vo;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class TreeVo implements Serializable {

    private static final long serialVersionUID = 749360940290141180L;

    private String id;

    private String parentId;

    private String text;

    private String title;

    private String value;

    private String icon;

    private Boolean showcheck;

    // 是否被勾选0 for unchecked, 1 for partial checked, 2 for checked
    private Integer checkstate;

    private Boolean hasChildren;

    private Boolean isexpand;

    private Boolean complete;

    private List<TreeVo> childNodes;

    /**
     * 两层递归实现建树
     */
    public static List<TreeVo> RecursiveBuilder(List<TreeVo> treeList) {

        List<TreeVo> lists = new ArrayList<>();
        for (TreeVo node : treeList) {
            if (node.getChildNodes() == null) {
                node.setChildNodes(new ArrayList<>());
            }
            if ("0".equals(node.getParentId())) {
                lists.add(node);
                node.setHasChildren(true);
            }
            for (TreeVo menu : treeList) {
                if (menu.getParentId().equals(node.getId())) {
                    node.getChildNodes().add(menu);
                    node.setHasChildren(true);
                }
            }
        }
        return lists;
    }

    /**
     * 两层递归实现建树
     */
    public static List<TreeVo> RecursiveBuilder(List<TreeVo> list, String parentId) {
        Map<String, List<TreeVo>> childrenMap = new HashMap<>();
        Map<String, TreeVo> parentMap = new HashMap<>();
        List<TreeVo> res = new ArrayList<>();

        //首先按照
        for (TreeVo node : list) {
            node.hasChildren = false;
            node.complete = true;
            // 注册子节点映射表
            if (!childrenMap.containsKey(node.parentId)) {
                childrenMap.put(node.parentId, new ArrayList<>());
            } else if (parentMap.containsKey(node.parentId)) {
                parentMap.get(node.parentId).hasChildren = true;
            }
            childrenMap.get(node.parentId).add(node);
            // 注册父节点映射节点表
            parentMap.put(node.id, node);

            // 查找自己的子节点
            if (!childrenMap.containsKey(node.id)) {
                childrenMap.put(node.id, new ArrayList<>());
            } else {
                node.hasChildren = true;
            }
            node.setChildNodes(childrenMap.get(node.id));
        }

        if (StringUtils.isEmpty(parentId)) {
            // 获取祖先数据列表
            for (Map.Entry<String, List<TreeVo>> entry : childrenMap.entrySet()) {
                if (!parentMap.containsKey(entry.getKey())) {
                    res.addAll(entry.getValue());
                }
            }
        } else {
            if (childrenMap.containsKey(parentId)) {
                return childrenMap.get(parentId);
            } else {
                return new ArrayList<>();
            }
        }
        return res;
    }
}
